import moment from 'moment'

/**************** 使用【插件】转换日期格式 ****************/
// 当前时间 格式化成日期格式  @returns {string}
export const formatNow = (fmt) => {
  if (fmt === undefined) fmt = 'YYYY-MM-DD HH:mm:ss'
  return moment().format(fmt)
}
// 时间戳转换成日期格式   @returns {string}
export const formatDate = (date, fmt) => {
  if (fmt === undefined) fmt = 'YYYY-MM-DD HH:mm:ss'
  return moment(date).format(fmt)
}
// 时间转时间戳   @returns {number}
export const getTimestamp = (dateStr) => {
  return moment(dateStr ? new Date(dateStr) : new Date()).unix()
}


/**************** 使用【原生】方法转换日期格式 ****************/
// 获取当前时间：年-月-日 时:分:秒 星期
export const currentDateTimeWeek = () => {
  const date = new Date();
  var year = date.getFullYear().toString().padStart(4, "0");
  var month = (date.getMonth() + 1).toString().padStart(2, "0");
  var day = date.getDate().toString().padStart(2, "0");
  var hours = date.getHours().toString().padStart(2,"0");
  var minutes = date.getMinutes().toString().padStart(2,"0");
  var seconds = date.getSeconds().toString().padStart(2,"0");
  var dayOfWeek = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六",][date.getDay()];
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} ${dayOfWeek}`
}
// 获取当前时间：年-月-日 时:分:秒
export const currentDateTime = () => {
  const date = new Date();
  var year = date.getFullYear().toString().padStart(4, "0");
  var month = (date.getMonth() + 1).toString().padStart(2, "0");
  var day = date.getDate().toString().padStart(2, "0");
  var hours = date.getHours().toString().padStart(2,"0");
  var minutes = date.getMinutes().toString().padStart(2,"0");
  var seconds = date.getSeconds().toString().padStart(2,"0");
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
// 获取当前时间：年月日
export const currentDate = () => {
  const date = new Date();
  var year = date.getFullYear().toString().padStart(4, "0");
  var month = (date.getMonth() + 1).toString().padStart(2, "0");
  var day = date.getDate().toString().padStart(2, "0");
  return `${year}${month}${day}`
}
// 获取当前时间：某年某月某日
export const currentYMD = () => {
  const date = new Date();
  var year = date.getFullYear().toString().padStart(4, "0");
  var month = (date.getMonth() + 1).toString().padStart(2, "0");
  var day = date.getDate().toString().padStart(2, "0");
  return `${year}年${month}月${day}日`
}
// 获取当前时间：某年某月
export const currentYM = () =>{
  const date = new Date();
  var year = date.getFullYear().toString().padStart(4, "0");
  var month = (date.getMonth() + 1).toString().padStart(2, "0");
  return `${year}年${month}月`
}
// 获取当前时间：时:分:秒
export const currentTime = () =>{
  const date = new Date();
  var hours = date.getHours().toString().padStart(2,"0");
  var minutes = date.getMinutes().toString().padStart(2,"0");
  var seconds = date.getSeconds().toString().padStart(2,"0");
  return `${hours}:${minutes}:${seconds}`
}
// 时间戳转日期格式 年-月-日 时:分:秒
export const getYMDHMS = (timestamp) =>{
  const time = new Date(timestamp)
  let year = time.getFullYear()
  let month = time.getMonth() + 1
  let date = time.getDate()
  let hours = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()

  if (month < 10) { month = '0' + month }
  if (date < 10) { date = '0' + date }
  if (hours < 10) { hours = '0' + hours }
  if (minute < 10) { minute = '0' + minute }
  if (second < 10) { second = '0' + second }
  return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
// 时间戳转日期格式：es6补0法
export const getYMDHMS0 = (timestamp) =>{
  const time = new Date(timestamp)
  let year = time.getFullYear()
  let month = (time.getMonth() + 1).toString().padStart(2, '0')
  let date = (time.getDate()).toString().padStart(2, '0')
  let hours = (time.getHours()).toString().padStart(2, '0')
  let minute = (time.getMinutes()).toString().padStart(2, '0')
  let second = (time.getSeconds()).toString().padStart(2, '0')
  return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
// 自定义日期格式化
export const dateFormat = (date, fmt) => {
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),         // 年
    "M+": (date.getMonth() + 1).toString(),      // 月
    "D+": date.getDate().toString(),             // 日
    "H+": date.getHours().toString(),            // 时
    "m+": date.getMinutes().toString(),          // 分
    "s+": date.getSeconds().toString(),          // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds()                  // 毫秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
  // 用法
  // let date = new Date()
  // dateFormat(date, "YYYY-MM-DD HH:mm:ss")
  // >>> 2019-06-06 19:45:23`
}

/**************** 日期计算 ****************/
// 计算当前日期的前一天
export const preDay = (dateTime) => {
  let date  = new Date(dateTime);
  date.setTime(date.getTime() - 1*24*60*60*1000);

  let year = date.getFullYear();
  let month = date.getMonth()+1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();

  if (month < 10) { month = '0' + month }
  if (day < 10) { day = '0' + day }
  if (hours < 10) { hours = '0' + hours }
  if (minutes < 10) { minutes = '0' + minutes }
  if (seconds < 10) { seconds = '0' + seconds }

  let preDay = year+"-"+month+"-"+day+"-"+" "+hours+":"+minutes+":"+seconds;
  return preDay
}
// 计算当前日期的后一天
export const nextDay = (dateTime) => {
  let date = new Date(dateTime);
  date.setTime(date.getTime() + 1*24*60*60*1000);

  let year = date.getFullYear();
  let month = date.getMonth()+1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();

  if (month < 10) { month = '0' + month }
  if (day < 10) { day = '0' + day }
  if (hours < 10) { hours = '0' + hours }
  if (minutes < 10) { minutes = '0' + minutes }
  if (seconds < 10) { seconds = '0' + seconds }

  let nextDay = year+"-"+month+"-"+day+"-"+" "+hours+":"+minutes+":"+seconds;
  return nextDay
}
// 计算两个日期之间的相差天数
export const getDaysBetween = (start,end) => {
  var startDate = Date.parse(start);
  var endDate = Date.parse(end);
  if (startDate>endDate){
      return 0;
  }
  if (startDate==endDate){
      return 1;
  }
  var days = (endDate - startDate)/(1*24*60*60*1000);
  return  days;
}
// 计算两个日期之间的所有日期
export const getdiffdate = (startdate, enddate) => {
  //初始化日期列表，数组
  var diffdate = new Array();
  var i=0;
  //开始日期小于等于结束日期,并循环
  while( startdate <= enddate ){
    diffdate[i] = startdate;
    //获取开始日期时间戳
    var startdate_ts = new Date(startdate).getTime();
    // console.log('当前日期：'+startdate + '--' +'当前时间戳：'+startdate_ts);
    
    //增加一天时间戳后的日期
    var currDate = new Date(startdate_ts + (24*60*60*1000));
    // console.log('增加一天后的时间戳：'+next_date + '--' + '增加一天后的日期：'+currDate);

    //拼接年月日，这里的月份会返回（0-11），所以要+1
    var nextDate_y = currDate.getFullYear();
    var nextDate_M = (currDate.getMonth()+1 < 10) ? '0'+(currDate.getMonth()+1) : (currDate.getMonth()+1);
    var nextDate_d = (currDate.getDate() < 10) ? '0'+currDate.getDate() : currDate.getDate();
    var nextDate_h = (currDate.getHours() < 10) ? '0'+currDate.getHours() : currDate.getHours();
    var nextDate_m = (currDate.getMinutes() < 10) ? '0'+currDate.getMinutes() : currDate.getMinutes();
    var nextDate_s = (currDate.getSeconds() < 10) ? '0'+currDate.getSeconds() : currDate.getSeconds();

    startdate = nextDate_y+"-"+nextDate_M+"-"+nextDate_d+" "+nextDate_h+":"+nextDate_m+":"+nextDate_s;

    //增加数组key
    i++;
  }
  return diffdate
}
// 计算当前时间是否在指定时间段之内
export const changeDate = (start, end) => {
  var dateTime = "";
  var currentDate = new Date();

  /********* date.valueOf()返回时间戳 ******************/
  var startDate = new Date(start);
  var endDate = new Date(end);
  // 当前时间小于开始时间
  if(currentDate.valueOf()<startDate.valueOf()){
    dateTime = "直播前";
  // 当前时间大于开始时间小于结束时间 
  } else if(currentDate.valueOf()>startDate.valueOf() && currentDate.valueOf() < endDate.valueOf()){
    dateTime = "直播中";
  // 当前时间大于结束时间
  } else if(currentDate.valueOf() > endDate.valueOf()){
    dateTime = "直播后";
  }

  /********* 当浏览器版本较低，不兼容时使用；date.getTime()：返回毫秒数 ******************/
  // var startDate = new Date(start.replace(/-/g, "/"));
  // var endDate = new Date(end.replace(/-/g, "/"));
  // 当前时间小于开始时间
  // if(currentDate.getTime()<startDate.getTime()){
  //   dateTime = "-1";
  // // 当前时间大于开始时间小于结束时间 
  // } else if(currentDate.getTime()>startDate.getTime() && currentDate.getTime() < endDate.getTime()){
  //   dateTime = "0";
  // // 当前时间大于结束时间
  // } else if(currentDate.getTime() > endDate.getTime()){
  //   dateTime = "1";
  // }
  return dateTime;
}
// 计算指定日期加 一天
export const getDateAndOne = (startDate) => {
  startDate = new Date(startDate);
  startDate =+ startDate + 1000*60*60*24;
  startDate = new Date(startDate);
  var year = startDate.getFullYear().toString().padStart(4, "0");
  var month = (startDate.getMonth() + 1).toString().padStart(2, "0");
  var day = startDate.getDate().toString().padStart(2, "0");
  var hours = startDate.getHours().toString().padStart(2,"0");
  var minutes = startDate.getMinutes().toString().padStart(2,"0");
  var seconds = startDate.getSeconds().toString().padStart(2,"0");
  var endDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  console.log(`一天后的时间：`,endDate);
  return endDate;
}
// 计算指定日期与当前日期的时间差
export const getDistanceSpecifiedTime = (date) => {
  let currentDate = new Date();//当前时间
  let specifiedTime = date;//指定时间
  let timeDifference  = currentDate.getTime() - new Date(specifiedTime).getTime();//时间差的毫秒数
  // console.log(timeDifference);

  //计算出相差天数
  let days = Math.floor(timeDifference/(24*3600*1000)); 
    
  //计算出小时数
  let leave1 = timeDifference % (24 * 3600 * 1000);//计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数  
  let leave2 = leave1 % (3600 * 1000);//计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000));
  //计算相差秒数
  let leave3 = leave2 % (60 * 1000);//计算分钟数后剩余的毫秒数  
  let seconds = Math.round(leave3 / 1000);
  // return days +"天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
  return days;
}
// 计算指定日期据当前日期的值    @returns {string}
export const getDay = (day) => {
  let today = new Date()
  let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
  today.setTime(targetday_milliseconds)
  let tYear = today.getFullYear()
  let tMonth = today.getMonth()
  let tDate = today.getDate()
  tMonth = doHandleMonth(tMonth + 1)
  tDate = doHandleMonth(tDate)
  return tYear + '-' + tMonth + '-' + tDate
}
export const doHandleMonth = (month) => {
  let m = month
  if (month.toString().length === 1) {
    m = '0' + month
  }
  return m
}
// 计算超时*天*时*分*秒
export const getDuration = (my_time) => {
  var days = my_time / 1000 / 60 / 60 / 24
  var daysRound = Math.floor(days)
  var hours = my_time / 1000 / 60 / 60 - 24 * daysRound
  var hoursRound = Math.floor(hours)
  var minutes =
      my_time / 1000 / 60 - 24 * 60 * daysRound - 60 * hoursRound
  var minutesRound = Math.floor(minutes)
  var seconds =
      my_time / 1000 -
      24 * 60 * 60 * daysRound -
      60 * 60 * hoursRound -
      60 * minutesRound
  var secondsRound = Math.floor(seconds)
  var time = '超时' + daysRound + '天' + hoursRound + '小时' + minutesRound + '分' + secondsRound + '秒';
  // var time = hoursRound + ':' + minutesRound + ':' + seconds
  return time
}
// 获取当月的第一天
export const getMounthFirstDay = () => {
  let now = new Date()
  let nowMonth = now.getMonth()
  let nowYear = now.getFullYear()
  let monthStartDate = new Date(nowYear, nowMonth, 1)
  return monthStartDate
}